package com.laicunba.ext.web.controller.console;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.laicunba.ext.web.controller.BaseController;
import com.laicunba.pojo.FeedBackDO;
import com.laicunba.pojo.FeedBackReplyDO;
import com.laicunba.pojo.param.Page;
import com.laicunba.pojo.vo.FeedBackVO;
import com.laicunba.service.FeedBackReplyService;
import com.laicunba.service.FeedBackService;
import com.laicunba.service.UserService;
import com.laicunba.util.StringUtil;

@Controller
@RequestMapping(value = "/console/ext")
public class FeedBackController extends BaseController {

	@Autowired
	private FeedBackService feedBackService;
	@Autowired
	private FeedBackReplyService feedBackReplyService;

	@Autowired
	private UserService userService;

	@ResponseBody
	@RequestMapping(value = "/feed_back_list.htm")
	public String feedBackList(FeedBackVO feedBackVO) {

		// FeedBackConditionVO condition = new FeedBackConditionVO();
		// condition.setLimit(50);
		// condition.setStatus(status);
		// condition.setMobile(mobile);
		// condition.setStatus(beginTimeStr);
		// condition.setMobile(endTimeStr);

		List<FeedBackVO> feedBackVOList = feedBackService.feedBackList(feedBackVO);
		return toJsonArray(feedBackVOList);
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/feed_back_update.htm")
	public String feedBackUpdate(String status, String id, String replyContent) {
		JSONObject json = new JSONObject();
		FeedBackDO feedBackDO = feedBackService.get(FeedBackDO.class, id);

		feedBackDO.setStatus("0".equals(status) ? "1" : "0");
		json.put("success", feedBackService.updateSkipCheckOwner(feedBackDO));
		FeedBackReplyDO replyDO = new FeedBackReplyDO();
		replyDO.setFeedBackId(id);
		replyDO.setContent(replyContent);
		replyDO.setReplyerSign("匿名客服");
		json.put("success", feedBackReplyService.create(replyDO));
		json.put("id", id);
		return json.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping(value = "/feed_back_user_list.htm")
	public String feedBackUserList(HttpServletRequest request) {
		Page page = new Page(1, 100);
		String status = request.getParameter("status");
		List<Map<String, Object>> feedBackUserList = feedBackService.findFeedBackUserList(StringUtil.isNotEmpty(status) ? status : "0", page);

		return toJsonArray(feedBackUserList);
	}

	@ResponseBody
	@RequestMapping(value = "/feed_back_user_list_more.htm")
	public String feedBackUserListMore(HttpServletRequest request) {
		JSONObject json = new JSONObject();
		String pageIndexStr = request.getParameter("pageIndex");
		Page page = new Page(StringUtil.isNotEmpty(pageIndexStr) ? Integer.parseInt(pageIndexStr) : 1, 100);
		String status = request.getParameter("status");
		List<Map<String, Object>> feedBackUserList = feedBackService.findFeedBackUserList(StringUtil.isNotEmpty(status) ? status : "0", page);
		
		return toJsonArray(feedBackUserList);
	}

	@ResponseBody
	@RequestMapping(value = "/user_chat_list.htm")
	public String userChatList(HttpServletRequest request) {
		List<Map<String,String>> chatlist = feedBackService.findNewChatByUid(request.getParameter("userId"), 200);
		return toJsonArray(chatlist);
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/reply_user_feed_back.htm")
	public String replyUserFeedBack(HttpServletRequest request) {
		JSONObject json = new JSONObject();
		String userId = request.getParameter("userId");
		String feedBackId = request.getParameter("feedBackId");
		String replyContent = request.getParameter("replyContent");
		if(StringUtil.isNotEmpty(userId, replyContent)) {
			FeedBackReplyDO replyDO = new FeedBackReplyDO();
			replyDO.setFeedBackId(feedBackId);
			replyDO.setContent(replyContent);
			replyDO.setReplyerSign("匿名客服");
			if(feedBackReplyService.create(replyDO) && feedBackService.updateFeedBackStatus(userId)) {
				json.put("code", "success");
				json.put("msg", "回复成功");
			} else {
				json.put("code", "fail");
				json.put("msg", "回复失败");
			}
		} else {
			json.put("code", "fail");
			json.put("msg", "用户不存在或回复内容为空");
		}
		return json.toJSONString();
	}
	
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/mark_user_feed_back.htm")
	public String markUserFeedBack(HttpServletRequest request) {
		JSONObject json = new JSONObject();
		String userId = request.getParameter("userId");
		if(StringUtil.isNotEmpty(userId)) {
			if(feedBackService.updateFeedBackStatus(userId)) {
				json.put("code", "success");
				json.put("msg", "标记成功");
			} else {
				json.put("code", "fail");
				json.put("msg", "标记失败");
			}
		} else {
			json.put("code", "fail");
			json.put("msg", "用户不存在或回复内容为空");
		}
		return json.toJSONString();
	}

}
